#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <math.h>
#include <stdint.h>
#include "cloud.h" 
#include "user.h"

void getFromCloud(CloudServer *c, User *u, int randomId, int hashnum) {
    for (int i = 0; i < TABLE_SIZE; i++) {
        if (c->user_o_hat_table[u->User_symbol]->ohat[i]->randomId == randomId) {
            for (int j = 0; j < BUCKET_SIZE + 1; j++) {
                u->Px->Px_coef[j] = c->user_o_hat_table[u->User_symbol]->ohat[i]->ox_coef[j] + u->my_z_table->ztable[hashnum]->zx_coef[j]; //将o_hat（x）中的元素发送给临时P(x)
            }
            u->Px->randomId = u->my_z_table->ztable[hashnum]->randomId;
            break;
        }
    }
}

void divide_and_send_polynomial(CloudServer *c, User *u, int hashnum) {
    //将P(x)秘密分享为两部分，并返回给云平台

    for (size_t j = 0; j < TABLE_SIZE; j++) //找到该用户要更新哪个桶
    {
        if (c->user_o_hat_table[u->User_symbol]->ohat[j]->randomId == u->Px->randomId) //找到对应的桶
        {
            for (size_t k = 0; k < BUCKET_SIZE + 1; k++) {
                c->user_o_hat_table[u->User_symbol]->ohat[j]->ox_coef[k] = rand();
                u->my_z_table->ztable[hashnum]->zx_coef[k] = u->Px->Px_coef[k] - c->user_o_hat_table[u->User_symbol]->ohat[j]->ox_coef[k];
            }
        }
    }
}

long long power(long long x, int n) {//求幂函数
    long long result = 1;

    // 快速幂计算
    while (n > 0) {
        if (n % 2 == 1) {  // 如果 n 是奇数
            result *= x;
        }
        x *= x;  // 将底数平方
        n /= 2;  // 指数减半
    }

    return result;
}